﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Tanks
{
    public class Missile
    {
        Direction facing;
        protected int range=2;
        protected int strength=10;
        protected Cell c;
        protected Game game = Game.Instance();

        public Missile(Direction Facing, Cell C)
        {
            facing = Facing;
            c = C;
        }
        protected virtual void drawShape(Bitmap canvas)
        {
            using (Graphics gr = Graphics.FromImage(canvas))
            {
                gr.FillEllipse(new SolidBrush(Color.Orange), new Rectangle(c.X * game.CellSize + game.CellSize / 5, c.Y * game.CellSize + game.CellSize / 5, game.CellSize / 5, game.CellSize / 5));
            }
        }
        public void Draw(Bitmap canvas)
        {
            Cell nextC=c;
            switch (facing){
                case Direction.North:
                    if (c.Y - 1 >= 0)
                    {
                        nextC = game.Grid[c.X, c.Y - 1];
                    }
                    break;
                case Direction.South:
                    if (c.Y + 1 < game.FieldHeight)
                    {
                        nextC = game.Grid[c.X, c.Y + 1];
                    }
                    break;
                case Direction.West:
                    if (c.X - 1 >= 0)
                    {
                        nextC = game.Grid[c.X - 1, c.Y];
                    }
                    break;
                case Direction.East:
                    if (c.X + 1 < game.FieldWidth)
                    {
                        nextC = game.Grid[c.X + 1, c.Y];
                    }
                    break;
            }
            if(nextC.currTank==null&&nextC.Block==BlockType.Empty&&c != nextC){
                c = nextC;
            }
            else
            {
                if (nextC.currTank == null)
                {
                    game.Explode(range, c, strength);
                }
                else
                {
                    game.Explode(range, nextC, strength);
                }
                game.Miss.Remove(this);
            }
            drawShape(canvas);
        }
    }
}
